package com.app.micaihu.view.home.exclusive.adapter.provider

import android.text.TextUtils
import android.util.SparseArray
import android.view.View
import android.widget.FrameLayout
import com.app.micaihu.R
import com.app.micaihu.ad.toutiao.ToutiaoAdManager
import com.app.micaihu.ad.toutiao.ToutiaoCloseDialog
import com.app.micaihu.bean.news.NewsEntity
import com.app.micaihu.view.home.exclusive.adapter.ExclusiveListAdapter
import com.blankj.utilcode.util.LogUtils
import com.bytedance.sdk.openadsdk.TTNativeExpressAd
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder

/**
 *
 */
class AdToutiaoProvider : BaseItemProvider<NewsEntity>() {

    override val itemViewType: Int
        get() = ExclusiveListAdapter.itemTypeToutiao

    override val layoutId: Int
        get() = R.layout.item_toutiao_express_ad

    private val ttNativeExpressAdSparseArray = SparseArray<TTNativeExpressAd?>()

    private val flushToutiao = false
    override fun convert(helper: BaseViewHolder, item: NewsEntity) {
        val frameLayout = helper.getView<FrameLayout>(R.id.view_express)
        val viewLine = helper.getView<View>(R.id.item_divider)

        if (frameLayout == null) return
        if (item == null) return
        viewLine.visibility = View.VISIBLE
        frameLayout.visibility = View.VISIBLE
        frameLayout.removeAllViews()
        val nativeAd = item.nativeAd ?: return
        var nativeAdSize = 0
        // 如果没有获取到广告则 从广告管家里获取一个新广告
        // 如果没有获取到广告则 从广告管家里获取一个新广告
        val adManager = ToutiaoAdManager.getInstance().getAd(nativeAd.adId)
        if (adManager == null) {
            ToutiaoAdManager.getInstance().putAd(nativeAd)
        } else {
            nativeAdSize = adManager.nativeAdSize
        }
        val position = helper.layoutPosition
        if (!flushToutiao) {
            val ttNativeExpressAd: TTNativeExpressAd? = ttNativeExpressAdSparseArray.get(position)
            ttNativeExpressAd?.let {
                fillToutiaoView(frameLayout, position, item, ttNativeExpressAd)
                return
            }
        }
//        LogUtils.e("------------------------------------------------->" + position + " flushToutiao->" + flushToutiao);
        // 如果广告缓存中有广告则开始获取
        //        LogUtils.e("------------------------------------------------->" + position + " flushToutiao->" + flushToutiao);
        // 如果广告缓存中有广告则开始获取
        if (nativeAdSize > 0) {
            val nrAd = adManager!!.nativeAd
            // 如果获取的广告不为null
            nrAd?.let { fillToutiaoView(frameLayout, position, item, it) }
        } else {
            val ad = ""
            //            if (AdConfigManagerNew.localeList != null && AdConfigManagerNew.localeList.size() > 0) {
//                for (NativeAd item : AdConfigManagerNew.localeList) {
//                    if (item != null && AdConfigManagerNew.TT_AD_TYPE.equals(item.getAdType())) {
//                        ad = item.getAdId();
//                        break;
//                    }
//                }
//            }
            //用获取到的广告id去获取预加载的广告
            if (!TextUtils.isEmpty(ad)) {
                val bna = ToutiaoAdManager.getInstance().getAd(ad)
                if (bna != null) {
                    val oneNrAd = bna.nativeAd
                    // 如果获取的广告不为null
                    if (oneNrAd != null) {
                        fillToutiaoView(frameLayout, position, item, oneNrAd)
                        return
                    }
                }
            }
            //如果最终还是没有获取到加载好的广告,则通过广告id去请求广告
            val bna = ToutiaoAdManager.getInstance().getAd(nativeAd.adId)
            bna?.loadOneAd(context, nativeAd.adId) { ad, success ->
                if (ad != null && success) {
                    fillToutiaoView(frameLayout, position, item, ad)
                }
            }
        }
    }

    private fun fillToutiaoView(frameLayout: FrameLayout, position: Int, newsEntity: NewsEntity?, nrAd: TTNativeExpressAd?) {
        if (newsEntity == null || nrAd == null) return
        ttNativeExpressAdSparseArray.put(position, nrAd)
        delDialogView(frameLayout, nrAd, position)
        val expressAdView = nrAd.expressAdView
        if (expressAdView != null && expressAdView.parent == null) {
            frameLayout.addView(expressAdView)
            //            nrAd.render();
        }
    }

    private fun delDialogView(frameLayout: FrameLayout, adView: TTNativeExpressAd, position: Int) {
        //使用自定义样式
        val words = adView.filterWords
        if (words == null || words.isEmpty()) {
            return
        }
        val toutiaoCloseDialog = ToutiaoCloseDialog(context, words)
        toutiaoCloseDialog.setOnDislikeItemClick { filterWord ->
            LogUtils.e("toutiao filterWord", filterWord.name, "del position->$position")
            //                LiveEventBus.get(Config.EventBus.delNewsListAd, Integer.class).post(position);
//                list.remove(newsEntity);
//                notifyDataSetChanged();
            frameLayout.visibility = View.GONE
        }
        toutiaoCloseDialog.setCancelable(true)
        toutiaoCloseDialog.setCanceledOnTouchOutside(true)
        adView.setDislikeDialog(toutiaoCloseDialog)
    }
}